import { getListApi } from "@/api/managementCenter/driverManagement/index";
import { getContractSelectListByOutIdApi } from "@/api/waybillManagement/outWarehouseReceipt";
import { ref, Ref } from "vue";

export default function InitHook(state: { passData: Ref<any> }) {
  const { passData } = state;
  const formState = ref<Partial<OutWarehouseReceiptManagement.AddDriverType>>({
    userDriverName: undefined,
    userDriverId: undefined,
    outWarehouseId: undefined,
    brokerAddress: undefined
  });
  const loading = ref<boolean>(false);

  const getDriverOption = ref<Array<{ label: string; value: string }>>([]);
  const getWareHouseAddressOption = ref<Array<{ label: string; value: string }>>([]);

  const formRef = ref<any>(null);

  //获取司机
  const init = async () => {
    getListApi({ pageNum: 1, pageSize: 100, total: 1 } as any).then((res: any) => {
      const { code, data } = res;
      const { records } = data;
      if (code === 200) {
        getDriverOption.value = records.map((t: any) => {
          return { label: t.username, value: t.userId };
        });
      }
    });
  };
  //获取仓库地址
  const getWarehouseAddress = () => {
    getContractSelectListByOutIdApi(passData.value.outWarehouseId ? passData.value.outWarehouseId : passData.value[0].outWarehouseId).then((res: any) => {
      const { data } = res;
      getWareHouseAddressOption.value = data.map((t: any) => {
        return { label: t.address, value: t.id };
      });
    });
  };
  return {
    formState,
    loading,
    init,
    formRef,
    getDriverOption,
    getWareHouseAddressOption,
    getWarehouseAddress
  };
}
